﻿DKPMCwlEngine = {}


local wl_responsetime = 60
local wl_frequency = 600
local wl_countDownTime = 60
local wl_UpdateInterval = 1.0
local wl_last_checked = 0;
local wl_raidStatus = false


--[[This function responds to the DKP Server with WL Accept in order for earning the waiting list DKP]]
function DKPMCwlEngine:respondToWaitingListQuery()
		if (zKP_Client_Variables.DkpLeader.name ~= "") then	
			wl_responsetime = wl_countDownTime;        
			local sender = zKP_Client_Variables.DkpLeader.name

			if (sender ~= nil and sender ) then	
				SendChatMessage("wl accept","WHISPER",nil,zKP_Client_Variables.DkpLeader.name)		
			end			
		end
end

function DKPMCwlEngine:Response(self, elapsed)
  self.TimeSinceLastUpdate = self.TimeSinceLastUpdate + elapsed; 	

    if (self.TimeSinceLastUpdate > wl_UpdateInterval ) then
        if (wl_responsetime <= wl_countDownTime and wl_responsetime > 0) then            
                wl_responsetime = wl_responsetime -1
                zKP_WaitingListFormTimeLabelText:SetText("You have "..wl_responsetime.." seconds to reply")                                                
        else
            zKP_WaitingListFormLabelText:SetText("To bad, you just lost 10 DKP. \nBe quicker on the wl accept next time.")					
        end  
        self.TimeSinceLastUpdate = 0
    end    
end

function DKPMCwlEngine:UpdateTimer(self, elapsed)
    self.TimeSinceLastUpdate = self.TimeSinceLastUpdate + elapsed;

    if (self.TimeSinceLastUpdate > wl_UpdateInterval and getglobal("DKPManagerWaitingListTimeFrameLabelTimer"):GetText() ~= "00:00") then    
    
        local tempTime = time() - wl_last_checked;
        
        if (tempTime < wl_frequency) then 
            tempTime = wl_frequency - tempTime;                                

			if (tempTime < 300 and tempTime > 120) then	-- when 5 minutes left to wl accept change color to yellow
				getglobal("DKPManagerWaitingListTimeFrameLabelTimer"):SetTextColor(1,1,0) -- R=1, G=1, B=0 turns the text yellow
            elseif (tempTime <= 120) then	-- when 3 minutes left change color to red as a warning for incomming w
				getglobal("DKPManagerWaitingListTimeFrameLabelTimer"):SetTextColor(1,0,0) -- R=1, G=0, B=0 turns the text red
			else
				getglobal("DKPManagerWaitingListTimeFrameLabelTimer"):SetTextColor(0,1,0) -- R=0, G=1, B=0 turns the text green
			end
			getglobal("DKPManagerWaitingListTimeFrameLabelTimer"):SetText(date("%M:%S",tempTime)) 

        else
            wl_last_checked = time(); 
            DKPManagerWaitingListTimeFrameLabelTimer:SetText("00:00"); 
        end
        self.TimeSinceLastUpdate = 0;
    end
        
    
end   

--[[This function is handling events that the frame is registrered for]]
function DKPMCwlEngine:TimerEvent(event, message, sender)

	if (zKP_Client_Variables.options.use_wl_engine == true) then	
		if (event == "CHAT_MSG_WHISPER") then	
			if(message == "<zKP>whisper me the following: wl accept") then       
				zKP_Client_Variables.DkpLeader.name = sender
				zKP_WaitingListFormTimeLabelText:SetText("You have "..wl_responsetime.." seconds to reply");       
				zKP_WaitingListForm:Show();       
				wl_responsetime = wl_countDownTime;
				DKPMCwlEngine:StartWLTimer();
			   
			elseif (string.find(message,"<zKP>You recieve: %d+dkp") ) then 
					DKPMCwlEngine:ResetWLTimers()				
			elseif (string.find(message,"<zKP>%d+sec to slow") ) then
					message = string.sub(message,string.find(message,"(%d+)") ,string.find(message,"sec")-1) 
				   DKPMCwlEngine:ResetWLTimers(tonumber(message))					
			end
		end
	end

	if (zKP_Client_Variables.options.use_wl_timer == true 
															and event == "RAID_INSTANCE_WELCOME" 
															and zKP_Client_Variables.options.use_wl_timer == true 
															and DKPMC:IsPlayerInRaid(zKP_Client_Variables.DkpLeader.name) ) then	

			DKPMCwlEngine:StopWLTimer()
	end
    
    
end

function DKPMCwlEngine:StartWLTimer(offset)
    
    if (zKP_Client_Variables.options.use_wl_timer == true) then
        wl_last_checked = time();
		if not (offset) then	
	        DKPManagerWaitingListTimeFrameLabelTimer:SetText("10:00");
		else
	        wl_last_checked = wl_last_checked -offset;
			DKPManagerWaitingListTimeFrameLabelTimer:SetText(date("%M:%S",(wl_frequency-offset) ) );
		end

        DKPManagerWaitingListTimeFrame:Show();
    end        
    
end    


function DKPMCwlEngine:StopWLTimer()
   if (zKP_Client_Variables.options.use_wl_timer == true) then
        wl_last_checked = 0;
        DKPManagerWaitingListTimeFrame:Hide();
   end
end    

function DKPMCwlEngine:ResetWLTimers(offset)
	DKPMCwlEngine:StopWLTimer();	
	DKPMCwlEngine:StartWLTimer(offset);	
end